package cn.xuanxuanyuhua.crm.web.controller;

import cn.xuanxuanyuhua.crm.domain.Customer;
import cn.xuanxuanyuhua.crm.query.CustomerEchartQueryObject;
import cn.xuanxuanyuhua.crm.query.CustomerQueryObject;
import cn.xuanxuanyuhua.crm.service.ICustomerEchartService;
import cn.xuanxuanyuhua.crm.service.ICustomerService;
import cn.xuanxuanyuhua.crm.service.ISystemDictionaryService;
import cn.xuanxuanyuhua.crm.util.JSONResult;
import cn.xuanxuanyuhua.crm.util.JSONUtil;
import cn.xuanxuanyuhua.crm.util.MessageUtil;
import cn.xuanxuanyuhua.crm.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customerEchart")
public class CustomerEchartController {
    @Autowired
    private ICustomerEchartService customerEchartService;

    @RequestMapping("/list")
    public String selectEchart(Model m, @ModelAttribute("qo")CustomerEchartQueryObject qo){
        m.addAttribute("list",customerEchartService.selectEchart(qo));
        return "/customerEchart/list";
    }

    @RequestMapping("/queryBar")
    public String queryBar(@ModelAttribute("qo")CustomerEchartQueryObject qo,Model m){
        List<Map<String, Object>> list = customerEchartService.selectEchart(qo);
        List<Object> groupType = new ArrayList<>();
        List<Object> totalNumber = new ArrayList<>();
        for (Map<String, Object> map : list) {
            groupType.add(map.get("groupType"));
            totalNumber.add(map.get("totalNumber"));
        }
        m.addAttribute("msg",MessageUtil.changMsg(qo));
        m.addAttribute("groupType", JSONUtil.toJSONString(groupType));
        m.addAttribute("totalNumber",JSONUtil.toJSONString(totalNumber));
        return "/customerEchart/bar";
    }

    @RequestMapping("/queryPie")
    public String queryPie(@ModelAttribute("qo")CustomerEchartQueryObject qo,Model m){
        List<Map<String, Object>> list = customerEchartService.selectEchart(qo);
        List<Object> groupType = new ArrayList<>();
        List<Map<String,Object>> totalNumber = new ArrayList<>();
        for (Map<String, Object> map : list) {
            Map<String,Object> data = new HashMap<>();
            groupType.add(map.get("groupType"));
            data.put("name",map.get("groupType"));
            data.put("value",map.get("totalNumber"));
            totalNumber.add(data);
        }
        m.addAttribute("msg",MessageUtil.changMsg(qo));
        m.addAttribute("groupType", JSONUtil.toJSONString(groupType));
        m.addAttribute("totalNumber",JSONUtil.toJSONString(totalNumber));
        return "/customerEchart/pie";
    }
}
